import Target1
import config
import db2

# 按间距中的绿色按钮以运行脚本。
import load

if __name__ == '__main__':
    schemesList = []  # 保存排课方案的总列表
    RTmatrix_filled = []  # 存储填充课程信息后的教室时间矩阵列表
    STmatrix_filled = []  # 保存学生时间矩阵列表
    TTmatrix_filled = []  # 保存老师时间矩阵列表

    timetable = db2.timeTable()
    teachers_list = db2.init_sel_teachers()
    students_list = db2.init_sel_students()
    classrooms = db2.init_classrooms()
    buildings_dis = db2.init_buildingDistance()

    students = [students_list[i][0] for i in range(len(students_list))]
    teachers = [teachers_list[i][0] for i in range(len(teachers_list))]

    RT, ST, TT = load.init_timetables(classrooms, students, teachers)
    for task in timetable:
        week_type = task[11]
        start_week = task[7]
        end_week = task[8]
        classroomList = task[12:]
        if week_type == 0:
            for i in range(start_week, end_week + 1):
                for classroom_time in classroomList:
                    classroom = classroom_time[0]
                    time_slot = classroom_time[1]
                    # ('GGJXL-G309', 60, 'GGJXL') is not in list
                    RT[i - 1][classrooms.index(classroom)][time_slot - 1] = (
                        task[0], classroom[1], len(task[6]))
                    TT[i - 1][teachers.index(task[3])][time_slot - 1] = (task[0], classroom)
                    for stu in task[6]:
                        ST[i - 1][students.index(stu)][time_slot - 1] = (task[0], classroom)
        if week_type == 1:
            for i in range(start_week, end_week + 1):
                if week_type % 2 == 1:
                    for classroom_time in classroomList:
                        classroom = classroom_time[0]
                        time_slot = classroom_time[1]
                        RT[i - 1][classrooms.index(classroom)][time_slot - 1] = (
                            task[0], classroom[1], len(task[6]))
                        TT[i - 1][teachers.index(task[3])][time_slot - 1] = (task[0], classroom)
                        for stu in task[6]:
                            ST[i - 1][students.index(stu)][time_slot - 1] = (task[0], classroom)
        if week_type == 1:
            for i in range(start_week, end_week + 1):
                if week_type % 2 == 0:
                    for classroom_time in classroomList:
                        classroom = classroom_time[0]
                        time_slot = classroom_time[1]
                        RT[i - 1][classrooms.index(classroom)][time_slot - 1] = (
                            task[0], classroom[1], len(task[6]))
                        TT[i - 1][teachers.index(task[3])][time_slot - 1] = (task[0], classroom)
                        for stu in task[6]:
                            ST[i - 1][students.index(stu)][time_slot - 1] = (task[0], classroom)

    schemesList.append(timetable)
    RTmatrix_filled.append(RT)  # 存储填充课程信息后的教室时间矩阵列表
    STmatrix_filled.append(ST)  # 保存学生时间矩阵列表
    TTmatrix_filled.append(TT)  # 保存老师时间矩阵列表

    fitness = Target1.target_fun(RTmatrix_filled, STmatrix_filled, TTmatrix_filled, buildings_dis, len(students), len(teachers))
    val = round(config.s_t_weight * fitness[0][0] + config.r_weight * fitness[0][1] + config.s_t_dis_weight *fitness[0][2], 2)
    print(val)